﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.Models;
using IFRS.DataAccess.DAL;
using System.Data;
using IFRS_Global;
using System.Globalization;
using System.Threading;

public partial class IFRS_WIN06_4A : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!(Page.IsPostBack || Page.IsCallback))
        {            
            this.AddClientScript();
            this.ViewMenu();
            this.BindDDL();
            this.SetErrorMessage();
            this.SetDefaultData();
            if (Request.QueryString["id"] != null)
            {
                this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }
    protected void btnSAVE_NEW_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }
    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData(false);
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN06_1S.aspx");
    }
    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsolutePath));
    }
    #endregion
    #region "---------- Methods -----------"
    private void AddClientScript()
    {
        btnCANCEL.Attributes["OnClick"] = "if(!confirm(\"" + Resources.ifrsResource.ConfirmCancel + "\")) return false;";

        btnCostCenter.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/CostCenter.aspx") + "','700','550');");
        btnGLAccountCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLAccount.aspx") + "','700','550');");
        btnCurrencyCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/Currency.aspx") + "','700','550');");        

        txtCostCenter.AddControl(hidCostCenter.ClientID);
        txtCostCenter.AddControl(txtCostCenter.ClientID);
        txtCostCenter.ControlSearch = txtCostCenter.ClientID;
        txtCostCenter.AddQueryString("bIsActive", "1");
        txtCostCenter.SearchKey = "VWCostCenter_oCOST_CENTER_CD_oCOST_CENTER_NM";
                
        txtGLAccountCode.AddControl(hidGLAccountCode.ClientID);
        txtGLAccountCode.AddControl(txtGLAccountCode.ClientID);
        txtGLAccountCode.ControlSearch = txtGLAccountCode.ClientID;
        txtGLAccountCode.AddQueryString("bIsActive", "1");
        txtGLAccountCode.SearchKey = "GLAccount_oGLAccountCode_oGLAccountName";

        txtCurrencyCode.AddControl(hidCurrencyCode.ClientID);
        txtCurrencyCode.AddControl(txtCurrencyCode.ClientID);
        txtCurrencyCode.ControlSearch = txtCurrencyCode.ClientID;
        txtCurrencyCode.AddQueryString("bIsActive", "1");
        txtCurrencyCode.SearchKey = "Currency_oCurrencyCode_oCurrencyName";
    }
    private void BindData(int ID)
    {
        List<GSBBond> gSBBond = new List<GSBBond>();
        DataTable dt = new SPR_GSBBond().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, gSBBond);
            
            txtTransactionDate.Text = DateTime.Now.DateToString();
            txtBondNo.Text = gSBBond[0].BondNo;
            txtCostCenter.Text = gSBBond[0].CostCenterName;
            txtGLAccountCode.Text = gSBBond[0].GLAccount;
            txtStartDate.Text = gSBBond[0].StartedDate.DateToString();
            txtEndDate.Text = gSBBond[0].EndDate.DateToString();
            ddlDayCountMethod.Text = gSBBond[0].DayCountMethodID.ToString();
            ddlPaymentDate.SelectedValue = gSBBond[0].PaymentDateID.ToString();
            txtPaymentFreq.Text = gSBBond[0].PaymentFreq.ToString();
            ddlPaymentFreqTimeBase.SelectedValue = gSBBond[0].PaymentFreqTimeBase;
            ddlPaymentCondition.SelectedValue = gSBBond[0].PaymentConditionID.ToString();
            txtCurrencyCode.Text = gSBBond[0].CurrencyCode;
            txtBondValue.Text = gSBBond[0].BondValue.ToString();
            txtAccountValue.Text = gSBBond[0].AccountValue.ToString();
            ddlEmbeddedOption.SelectedValue = gSBBond[0].EmbeddedOptionID.ToString();
            ddlPriceValueUnit.SelectedValue = gSBBond[0].PriceValueUnitID.ToString();
            ddlLicenseStatus.SelectedValue = gSBBond[0].LicenseStatusID.ToString();
            txtPriceValue.Text = gSBBond[0].PriceValue.ToString();           
            txtStatus.Text = "สร้างรายการ";         

            hidGSBBondID.Value = gSBBond[0].GSBBondID.ToString();
            hidUpdateDate.Value = gSBBond[0].UpdatedDate.ToString();
            
            hidCostCenter.Value = gSBBond[0].CostCenter;
            hidGLAccountCode.Value = gSBBond[0].GLAccount;
            hidCurrencyCode.Value = gSBBond[0].CurrencyCode;   
        }
    }
    private void BindDDL()
    {
        ddlDayCountMethod.SetData(DDL_TYPE.DayCountMethod, "", false, true);
        ddlPaymentDate.SetData(DDL_TYPE.PaymentDate, "", false, true);
        ddlPaymentFreqTimeBase.SetData(DDL_TYPE.PaymentFrequencyTimeBase, "", false, true);
        ddlPaymentCondition.SetData(DDL_TYPE.PaymentCondition, "", false, true);
        ddlEmbeddedOption.SetData(DDL_TYPE.EmbeddedOption, "", false, true);
        ddlPriceValueUnit.SetData(DDL_TYPE.PriceValueUnit, "", false, true);
        ddlLicenseStatus.SetData(DDL_TYPE.LicenseStatus, "", false, true);                    
    }    
    private void SetDefaultData()
    {
        txtStatus.Text = "สร้างรายการ";
        txtCurrencyCode.Text = "THB";
        hidCurrencyCode.Value = "THB";
    }
    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        btnSAVE_NEW.ValidationGroup = "Master";
        reqTransactionDate.SetRequiredField(lblTransactionDate, txtTransactionDate, btnSAVE);
        reqBondNo.SetRequiredField(lblBondNo, txtBondNo, btnSAVE);        
        reqCostCenter.SetRequiredField(lblCostCenter, txtCostCenter, btnSAVE);
        reqGLAccountCode.SetRequiredField(lblGLAccountCode, txtGLAccountCode, btnSAVE);
        reqStartDate.SetRequiredField(lblStartDate, txtStartDate, btnSAVE);
        reqEndDate.SetRequiredField(lblEndDate, txtEndDate, btnSAVE);
        reqAccountValue.SetRequiredField(lblAccountValue, txtAccountValue, btnSAVE);     
    }
    private void SaveData(bool SaveNew)
    {
        try
        {
            GSBBond gSBBond = new GSBBond();

            gSBBond.TransactionDate = txtTransactionDate.Text.StringToDateAllowNull();
            gSBBond.BondNo = txtBondNo.Text;
            gSBBond.CostCenter = hidCostCenter.Value;
            gSBBond.GLAccount = txtGLAccountCode.Text;
            gSBBond.StartedDate = txtStartDate.Text.StringToDate();
            gSBBond.EndDate = txtEndDate.Text.StringToDate();
            gSBBond.DayCountMethodID = ddlDayCountMethod.SelectedValue.AsIntIsnull();
            gSBBond.PaymentDateID = ddlPaymentDate.SelectedValue.AsIntIsnull();
            gSBBond.PaymentFreq = txtPaymentFreq.Text.AsDecimal();
            gSBBond.PaymentFreqTimeBase = ddlPaymentFreqTimeBase.SelectedValue;
            gSBBond.PaymentConditionID = ddlPaymentCondition.SelectedValue.AsIntIsnull();
            gSBBond.CurrencyCode = txtCurrencyCode.Text;
            gSBBond.BondValue = txtBondValue.Text.AsDecimal().Value;
            gSBBond.AccountValue = txtAccountValue.Text.AsDecimal().Value;
            gSBBond.EmbeddedOptionID = ddlEmbeddedOption.SelectedValue.AsIntIsnull();
            gSBBond.PriceValueUnitID = ddlPriceValueUnit.SelectedValue.AsIntIsnull();
            gSBBond.LicenseStatusID = ddlLicenseStatus.SelectedValue.AsIntIsnull();
            gSBBond.PriceValue = txtPriceValue.Text.AsDecimal().Value;
            gSBBond.RP_GSB_PRDCD = DefaultValue.RP_GSB_PRDCD.GSB_BOND;
            gSBBond.RP_SOURCE_FILE = DefaultValue.RP_SOURCE_FILE.MANUAL_GSB_BOND;
            gSBBond.RP_MAPPING_CD = DefaultValue.RP_MAPPING_CD.MANUAL_GSB_BOND;
            gSBBond.IsConfirmed = false;     // 0 - สร้างรายการ, 1 - ยืนยัน
            gSBBond.SetStandardField();
                      
            int iID = new SPR_GSBBond().INSERT(gSBBond);

            if (iID > 0)
            {
                if (SaveNew)
                {
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(Request.Url.AbsolutePath));
                }
                else
                {
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN06_2E.aspx?&id={0}", iID)));
                }
            }
            else
            {
                if (iID == -1)
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                }
                else
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN06_4A;
    }
    #endregion
}